<?php

class router {
	private $registry;
	
	private $path;
	
	private $args = array ();
	
	private $view_link;
	
	public $file;
	
	public $controller;
	
	public $action;
	
	//store parametrs for action
	public $action_param = array ();
	
	function __construct($registry) {
		$this->registry = $registry;
	}
	
	/**
	 *
	 * @set controller directory path
	 *
	 *
	 */
	function setPath($path) {
		
		/*** check if path i sa directory ***/
		if (is_dir ( $path ) == false) {
			throw new Exception ( 'Invalid controller path: `' . $path . '`' );
		}
		/*** set the path ***/
		$this->path = $path;
	}
	
	/**
	 *
	 * @load the controller
	 *
	 */
	public function loader() {
		/*** check the route ***/
		$this->getController ();
		/*** if the file is not there diaf ***/
		if (is_readable ( $this->file ) == false) {
			if (is_readable ( $this->file ) == false) {
			
			}
			$this->file = $this->path . '/error404.php';
			$this->controller = 'error404';
			
			$this->view_link = 'error/index';
		}
		
		/*** include the controller ***/
		include $this->file;
		
		/*** a new controller class instance ***/
		$class = $this->controller . 'Controller';
		$controller = new $class ( $this->registry );
		
		/*** check if the action is callable ***/
		if (is_callable ( array ($controller, $this->action ) ) == false) {
			$action = 'index';
		} else {
			$action = $this->action;
		}
		
		//use template for vieweng page from controller
		$this->registry->template = new template ( $this->registry, $this->view_link );
		
		/*** run the action ***/
		$controller->$action ();
		
		//show template
		//show() method from template)))
		$this->registry->template->show ();
	}
	
	private function getController() {
		//TODO not correct working .htaccess
		//if return empty $_GET['rt] if it's 'index', and doesn't depend on other values
		

		/*** get the route from the url ***/
		$route = (empty ( $_GET ['rt'] )) ? '' : $_GET ['rt'];
		
		if (empty ( $route )) {
			$route = 'index';
		} else {
			/*** get the parts of the route ***/
			$parts = explode ( '/', $route );
			$this->controller = $parts [0];
			if (isset ( $parts [1] )) {
				$this->action = $parts [1];
			}
			
			foreach ( $parts as $key => $part ) {
				//check if it's not a controller or action
				if ($key > 1) {
					//check if that's key or part
					if ($key % 2 == 0) {
						$prev_part = $part;
					
					} else {
						//set current key, as previuos value, for 'key'=>'value'
						$this->action_param [$prev_part] = $part;
					}
				}
			}
		}
		
		if (empty ( $this->controller )) {
			$this->controller = 'index';
		}
		
		/*** Get action ***/
		if (empty ( $this->action )) {
			$this->action = 'index';
		}
		
		$this->view_link = $this->controller . '/' . $this->action;
		
		/*** set the file path ***/
		$this->file = $this->path . '/' . $this->controller . 'Controller.php';
	}
	
	//TODO add validator
	public function getParam($key) {
		if (isset ( $this->action_param [$key] )) {
			return $this->action_param [$key];
		}
	}
	
	public function getPostValue($key) {
		if (isset ( $_POST [$key] )) {
			$val = $_POST [$key];
			$val = strip_tags ( $val );
			return $val;
		}
	}
	
	public function redirect($controller, $action, $param_array = null) {
		$location = "Location: " . __HOST_PATH . "/" . $controller . "/" . $action;
		
		foreach ( $param_array as $key => $value ) {
			$location .= "/" . $key . "/" . $value;
		}
		
		header ( $location );
	}
	
	/**
	 * 
	 * use for getting link url
	 * @param array with key=>value
	 */
	public function url($param_array) {
		$link = __HOST_PATH;
		
		foreach ( $param_array as $key => $value ) {
			$link .= '/' . $key . '/' . $value;
		}
		return $link;
	
	}
}

?>
